package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequiresRoles(value = "admin")
    @RequestMapping("getAll")
    @ResponseBody
    @LogAnnotation(content = "调用了查询所有用户方法")
    public Map getAll(@RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "3") Integer limit, User user) {
        HashMap map = new HashMap();
        try {
            PageInfo<User> pageInfo = userService.findAll(page, limit, user);
            System.out.println("=========");
            List<User> users = pageInfo.getList();
            int count = (int) pageInfo.getTotal();
            map.put("code", 0);
            map.put("data", users);
            map.put("count", count);
            return map;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("getOne")
    @ResponseBody
    public User getUser(User user) {
        try {
            return userService.findUserByFields(user);
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("addOne")
    @ResponseBody
    @LogAnnotation(content = "调用添加用户的方法", type = "add")
    public Boolean addUser(User user) {
        try {
            userService.addUser(user);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("delById")
    @ResponseBody
    @LogAnnotation(content = "调用删除用户的方法", type = "delete")
    public Boolean deleteUserById(User user) {
        try {
            userService.removeUserById(user.getUserId());
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("delBulk")
    @ResponseBody
    @LogAnnotation(content = "调用批量删除用户的方法", type = "deleteMultiple")
    public Boolean deleteMultiple(Integer[] ids) {
        try {
            userService.removeMultiple(ids);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("update")
    @ResponseBody
    @LogAnnotation(content = "调用修改用户的方法", type = "update")
    public Boolean updateUser(User user) {
        try {
            userService.renovateUser(user);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("getEcharts")
    @ResponseBody
    public Map getEcharts() {
        Map map = new HashMap();
        try {
            map = userService.findUserEcharts();
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
